use eframe::egui;
use egui::viewport::ViewportBuilder;
use eframe::NativeOptions;
use eframe::Theme;
use eframe::HardwareAcceleration;
use eframe::Renderer;
use eframe::EventLoopBuilderHook;
use eframe::WindowBuilderHook;
use egui_glow::ShaderVersion;
use egui_wgpu::WgpuConfiguration;
use eframe::UserEvent;
use eframe::EventLoopBuilder;
use egui::viewport::IconData;

use std::sync::Arc;
use std::path::Path;

fn main() {
  let mut native_options = eframe::NativeOptions::default();
  native_options.default_theme=Theme::Light;
  native_options.viewport.icon = icon_data();
  /*
   * let native_options = NativeOptions {

    viewport: ViewportBuilder::default(),
    vsync: false,
    multisampling: 4u16,
    depth_buffer: 0u8,
    stencil_buffer: 0u8,
    hardware_acceleration: HardwareAcceleration::Off,
    renderer: Renderer::default(),
    follow_system_theme: true,
    default_theme: Theme::Light,
    run_and_return: true,
    event_loop_builder: Some(Box::new(|builder: &mut EventLoopBuilder<UserEvent>| {EventLoopBuilderHook::default();})),
    window_builder: Some(Box::new(|builder: &mut ViewportBuilder| {WindowBuilderHook::default();})),
    shader_version: Some(ShaderVersion::Gl120),
    centered: true,
    //wgpu_options: WgpuConfiguration::default(),
    persist_window: false,
  };
  */
  eframe::run_native("My First Demo", native_options, Box::new(|cc| Box::new(MyEguiApp::new(cc))));
}

#[derive(Default)]
struct MyEguiApp {}

impl MyEguiApp {
    fn new(cc: &eframe::CreationContext<'_>) -> Self {
        // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals.
        // Restore app state using cc.storage (requires the "persistence" feature).
        // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
        // for e.g. egui::PaintCallback.
        Self::default()
    }
}

impl eframe::App for MyEguiApp {
   fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
     egui::SidePanel::right("my_right_panel")
          .default_width(80f32)
          .max_width(250f32)
          .show(ctx, |ui| {
            ui.label("Hello World!");
     });  
     egui::CentralPanel::default().show(ctx, |ui| {
           ui.heading("Hello World!");
       });
   }
}

fn icon_data() -> Option<Arc<IconData>> {
    let rgba = image::open(Path::new("./idea_841743.png")).expect("failed to open the image!").into_rgba8().into_raw();
    let icon_data = IconData{
        rgba,
        width: 64u32,
        height: 64u32,
    };
    Some(Arc::new(icon_data))
}
